﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Surging.Core.Consul.Configurations
{
    public class ConfigInfo
    {
        /// <summary>
        /// 初始化会话超时为20秒的consul配置信息。
        /// </summary>
        /// <param name="connectionString">连接字符串。</param>
        public ConfigInfo(string connectionString,string routePath = "services/serviceRoutes/", string commandPath = "services/serviceCommands/") :
            this(connectionString, TimeSpan.FromSeconds(20), routePath, commandPath)
        {
        }

        /// <summary>
        /// 初始化consul配置信息。
        /// </summary>
        /// <param name="connectionString">连接字符串。</param>
        /// <param name="sessionTimeout">会话超时时间。</param>
        public ConfigInfo(string connectionString, TimeSpan sessionTimeout,string routePath = "services/serviceRoutes/", string commandPath = "services/serviceCommands/")
        {
            
            SessionTimeout = sessionTimeout;
            RoutePath = routePath;
            CommandPath = commandPath;
            var  address= connectionString.Split(":");
            if(address.Length>1)
            {
                int port;
                int.TryParse(address[1], out port);
                Host = address[0];
                Port = port;
            }
        }

        public ConfigInfo(string host, int port) : this(host, port, TimeSpan.FromSeconds(20))
        {
        }

        public ConfigInfo(string host, int port, TimeSpan sessionTimeout)
        {
            SessionTimeout = sessionTimeout;
            Host = host;
            Port = port;
        }

        /// <summary>
        /// watch 时间间隔
        /// </summary>
        public int WatchInterval { get; set; } = 60;

        /// <summary>
        /// 命令配置路径
        /// </summary>
        public string CommandPath { get; set; }

        /// <summary>
        /// 路由配置路径。
        /// </summary>
        public string RoutePath { get; set; }

        public string Host { get; set; }

        public int Port { get; set; }

        /// <summary>
        /// 会话超时时间。
        /// </summary>
        public TimeSpan SessionTimeout { get; set; }

    }
}
